/**
 * Title :	Property view
 * Description: Property editor
 * Version:	provider
 * Copyright (c) 2001-2008
 * Terms of use:see license agreement at http://treebolic.sourceforge.net/en/license.htm
 * Author: Bernard Bou
 * Company: bsys
 */
package bsys.propertyview;

import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import java.net.MalformedURLException;
import java.net.URL;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.SwingConstants;
import javax.swing.table.DefaultTableCellRenderer;

import bsys.propertyview.PropertyView.Attribute;
import bsys.propertyview.PropertyView.AttributeType;

class Renderer extends DefaultTableCellRenderer
{
	private static final long serialVersionUID = 1L;

	/**
	 * View
	 */
	private final PropertyView thePropertyView;

	/**
	 * Color panel
	 */
	private JPanel theColorPanel = new JPanel();
	
	
	/**
	 * Alternate row column
	 */
	Color theAltRowColor = new Color(0xf0f0f0);

	/**
	 * The bold font
	 */
	private Font theBoldFont = new Font("Dialog", Font.BOLD, 12); //$NON-NLS-1$

	/**
	 * The italic font
	 */
	private Font theItalicFont = new Font("Dialog", Font.ITALIC, 12); //$NON-NLS-1$

	// icons

	/**
	 * Mandatory attribute icon
	 */
	private ImageIcon theMandatoryIcon = new ImageIcon(Renderer.class.getResource("images/mandatory.png")); //$NON-NLS-1$

	/**
	 * Optional attribute icon
	 */
	private ImageIcon theOptionalIcon = new ImageIcon(Renderer.class.getResource("images/optional.png")); //$NON-NLS-1$

	/**
	 * Attribute icon
	 */
	private ImageIcon theAttributeIcon = new ImageIcon(Renderer.class.getResource("images/attribute.png")); //$NON-NLS-1$

	/**
	 * Id icon
	 */
	private ImageIcon theIdIcon = new ImageIcon(Renderer.class.getResource("images/id.png")); //$NON-NLS-1$

	/**
	 * Id reference icon
	 */
	private ImageIcon theRefIdcon = new ImageIcon(Renderer.class.getResource("images/node.png")); //$NON-NLS-1$

	/**
	 * Text icon
	 */
	private ImageIcon theTextIcon = new ImageIcon(Renderer.class.getResource("images/text.png")); //$NON-NLS-1$

	/**
	 * Long text icon
	 */
	private ImageIcon theLongTextIcon = new ImageIcon(Renderer.class.getResource("images/longtext.png")); //$NON-NLS-1$

	/**
	 * Image icon
	 */
	private ImageIcon theImageIcon = new ImageIcon(Renderer.class.getResource("images/image.png")); //$NON-NLS-1$

	/**
	 * Link icon
	 */
	private ImageIcon theLinkIcon = new ImageIcon(Renderer.class.getResource("images/link.png")); //$NON-NLS-1$

	/**
	 * Color icon
	 */
	private ImageIcon theColorIcon = new ImageIcon(Renderer.class.getResource("images/color.png")); //$NON-NLS-1$

	/**
	 * Stroke icon
	 */
	private ImageIcon theStrokeIcon = new ImageIcon(Renderer.class.getResource("images/stroke.png")); //$NON-NLS-1$

	/**
	 * Terminator icon
	 */
	private ImageIcon theTerminatorIcon = new ImageIcon(Renderer.class.getResource("images/terminator.png")); //$NON-NLS-1$

	/**
	 * Font icon
	 */
	private ImageIcon theFontIcon = new ImageIcon(Renderer.class.getResource("images/font.png")); //$NON-NLS-1$

	/**
	 * Font size icon
	 */
	private ImageIcon theFontSizeIcon = new ImageIcon(Renderer.class.getResource("images/fontsize.png")); //$NON-NLS-1$

	/**
	 * Boolean icon
	 */
	private ImageIcon theBooleanIcon = new ImageIcon(Renderer.class.getResource("images/boolean.png")); //$NON-NLS-1$

	/**
	 * Constructor
	 * 
	 * @param thisPropertyView
	 *        property view
	 */
	public Renderer(PropertyView thisPropertyView)
	{
		thePropertyView = thisPropertyView;
		theColorPanel = new JPanel();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.swing.table.DefaultTableCellRenderer#getTableCellRendererComponent(javax.swing.JTable, java.lang.Object, boolean, boolean, int, int)
	 */
	public Component getTableCellRendererComponent(JTable table, Object thisValue, boolean isSelected, boolean hasFocus, int thisRow, int thisColumn)
	{
		setIcon(null);
		setFont(null);
		setForeground(Color.BLACK);
		setBackground(thisRow % 2 == 0 ? Color.WHITE : theAltRowColor);

		switch (thisColumn)
		{
		case PropertyView.VALUE:
		{
			Attribute thisAttribute = (Attribute) thisValue;
			Object thisAttributeValue = thisAttribute.theValue;
			if (thisAttributeValue == null)
			{
				setText(PropertyView.theDefaultString);
				return this;
			}

			AttributeType thisType = thisAttribute.theDescriptor.theType;
			switch (thisType)
			{
			case COLOR:
				theColorPanel.setBackground((Color) thisAttributeValue);
				return theColorPanel;

			case LABEL:
				setFont(theBoldFont);
				setText(thisAttributeValue.toString());
				setBackground(Color.DARK_GRAY);
				setForeground(Color.WHITE);
				return this;

			case TEXT:
			case LONGTEXT:
			case BOOLEAN:
			case DOUBLE:
			case INTEGER:
				setText(thisAttributeValue.toString());
				return this;

			case ID:
				setText((String) thisAttributeValue);
				return this;

			case REFID:
				setText((String) thisAttributeValue);
				setIcon(theRefIdcon);
				return this;

			case LINK:
				setText(PropertyView.decode((String) thisAttributeValue));
				setForeground(Color.BLUE);
				setFont(theItalicFont);
				setIcon(theLinkIcon);
				return this;

			case FONTFACE:
				setText((String) thisAttributeValue);
				setFont(new Font((String) thisAttributeValue, Font.PLAIN, 18));
				return this;

			case IMAGE:
				String thisImageFile = (String) thisAttributeValue;
				setIcon(makeIcon(thisImageFile));
				setText(thisImageFile);
				return this;

			case FONTSIZE:
				setIcon(theFontSizeIcon);
				setText(thisAttributeValue.toString());
				return this;

			case STROKE:
				setIcon(PropertyView.theStrokeIcons.get((String) thisAttributeValue));
				setText((String) thisAttributeValue);
				setForeground(Color.BLUE);
				return this;

			case TERMINATOR:
				setIcon(PropertyView.theTerminatorIcons.get((String) thisAttributeValue));
				setText((String) thisAttributeValue);
				setForeground(Color.BLUE);
				return this;

			default:
				System.err.println("Renderer doesn't handle this class: " + thisAttribute.theDescriptor.theName + " type:" + thisAttribute.theDescriptor.theType); //$NON-NLS-1$ //$NON-NLS-2$
				return super.getTableCellRendererComponent(table, thisValue, isSelected, hasFocus, thisRow, thisColumn);
			}
		}

		case PropertyView.STATUS:
		{
			Boolean thisBooleanValue = (Boolean) thisValue;
			setIcon(thisBooleanValue.booleanValue() ? theMandatoryIcon : theOptionalIcon);
			setText(null);
			setHorizontalAlignment(SwingConstants.CENTER);
			return this;
		}

		case PropertyView.TYPE:
		{
			AttributeType thisPropertyTypeValue = (AttributeType) thisValue;
			switch (thisPropertyTypeValue)
			{
			case TEXT:
				setIcon(theTextIcon);
				break;

			case LONGTEXT:
				setIcon(theLongTextIcon);
				break;

			case IMAGE:
				setIcon(theImageIcon);
				break;

			case LINK:
				setIcon(theLinkIcon);
				break;

			case COLOR:
				setIcon(theColorIcon);
				break;

			case BOOLEAN:
				setIcon(theBooleanIcon);
				break;

			case ID:
				setIcon(theIdIcon);
				break;

			case REFID:
				setIcon(theRefIdcon);
				break;

			case STROKE:
				setIcon(theStrokeIcon);
				break;

			case TERMINATOR:
				setIcon(theTerminatorIcon);
				break;

			case FONTFACE:
				setIcon(theFontIcon);
				break;

			case FONTSIZE:
				setIcon(theFontSizeIcon);
				break;

			default:
				setIcon(theAttributeIcon);
				break;
			}
			setText(null);
			setHorizontalAlignment(SwingConstants.CENTER);
			return this;
		}

		case PropertyView.NAME:
		{
			String thisName = (String) thisValue;
			setText(thisName);
			return this;
		}
		}
		return super.getTableCellRendererComponent(table, thisValue, isSelected, hasFocus, thisRow, thisColumn);
	}

	/**
	 * Make icon
	 * 
	 * @param thisImageFile
	 *        image file
	 * @return icon
	 */
	private Icon makeIcon(String thisImageFile)
	{
		try
		{
			URL thisUrl = new URL(thePropertyView.getImageRepository(), thisImageFile);
			return new ImageIcon(thisUrl);
		}
		catch (MalformedURLException e)
		{
			return null;
		}
	}
}